//
//  ArticleTypeView.m
//  Village
//
//  Created by 程虎 on 2017/9/8.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "ArticleTypeView.h"
#import "PostedType.h"

@interface ArticleTypeView()

@property (nonatomic, assign) NSInteger TypeIndex;
@property (nonatomic, strong) UIViewController *controllers;
@property (nonatomic, strong) NSArray *dataAry;
@end

@implementation ArticleTypeView

- (instancetype)initWithShowView:(UIViewController *)controllers WithData:(NSArray *)dataAry
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
        self.controllers = controllers;
        self.dataAry = [PostedType mj_objectArrayWithKeyValuesArray:dataAry];
        self.TypeIndex = -1;
        [self set_UI];
    }
    return self;
}

- (void)set_UI
{
    UIImageView *bkImg = [UIImageView new];
    bkImg.userInteractionEnabled = YES;
    bkImg.image = [UIImage imageNamed:@"BG_posted"];
    
    UILabel *lab = [[Tools shareInstance] creat_Label:@"告诉我们\n你要发布什么类型的文章" Color:[UIColor whiteColor] Font:iPhone5 ? 24 : 27 tag:-1 Alignment:0 Lines:0];
    
    UIButton *closeBtn = [[Tools shareInstance] creat_Button:@"" Color:[UIColor whiteColor] BackColors:[UIColor clearColor] Font:16 tag:-1];
    [closeBtn setImage:[UIImage imageNamed:@"close_Posted"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(CloseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *itemsView  = [self creatItemsView];
    
    UIButton *nextBtn = [[Tools shareInstance] creat_Button:@"下一步" Color:[UIColor whiteColor] BackColors:KRedColors Font:18 tag:-1];
    [nextBtn addTarget:self action:@selector(NextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    LRViewRadius(nextBtn, 5, 0.5, [UIColor clearColor]);
    
    [bkImg sd_addSubviews:@[lab,closeBtn,itemsView,nextBtn]];
    [self addSubview:bkImg];
    
    bkImg.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    CGFloat left = 15.5;
    CGFloat top = 72;

    if (iPhone5) {
        top = 60;
    }
    
    lab.sd_layout
    .leftSpaceToView(bkImg, left)
    .topSpaceToView(bkImg, top)
    .rightSpaceToView(bkImg, left)
    .autoHeightRatio(0);

    closeBtn.sd_layout
    .rightSpaceToView(bkImg, 20)
    .topSpaceToView(bkImg, 35)
    .widthIs(21)
    .heightEqualToWidth();
    
    itemsView.sd_layout
    .leftSpaceToView(bkImg, 13)
    .topSpaceToView(lab, 30)
    .rightSpaceToView(bkImg, 13);
    
    nextBtn.sd_layout
    .centerXEqualToView(bkImg)
    .topSpaceToView(itemsView, 55)
    .widthIs(200)
    .heightIs(44);
    
}

- (UIView *)creatItemsView
{
    UIView *views = [UIView new];
    views.backgroundColor = [UIColor clearColor];
    
    CGFloat margin = 15 * AutoScaleX;
    
    NSMutableArray *temp = [NSMutableArray array];
    for (int i = 0; i < self.dataAry.count; i++) {
        UIButton *btn = [[Tools shareInstance] creat_Button:@"" Color:nil BackColors:nil Font:0 tag:i + 10];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d_posted_off",i+ 2]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [views addSubview:btn];
        [temp addObject:btn];
        btn.sd_layout.heightEqualToWidth();
    }
    [views setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:3 verticalMargin:margin horizontalMargin:margin verticalEdgeInset:0 horizontalEdgeInset:0];
    
    return views;
}

#pragma mark - 关闭
- (void)CloseBtnClick:(UIButton *)buttons
{
    [self removeFromSuperview];
}

#pragma mark - 下一步
- (void)NextBtnClick
{
    if (self.TypeIndex != -1) {
        if (self.nextBlock) {
            self.nextBlock(self.dataAry[self.TypeIndex - 10]);
            [self removeFromSuperview];
        }
    }
    else {
        [MBProgressHUD showError:@"请选择您即将发布的文章类型！" toView:self];
    }
}

- (void)itemBtnClick:(UIButton *)buttons
{
    [buttons setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld_posted_on",buttons.tag - 10 + 2]] forState:UIControlStateNormal];
    if (self.TypeIndex == -1) {
        self.TypeIndex = buttons.tag;
    }
    else {
        UIButton *btn = (UIButton *)[self viewWithTag:self.TypeIndex];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld_posted_off",self.TypeIndex - 10 + 2]] forState:UIControlStateNormal];
        self.TypeIndex = buttons.tag;
    }
}
@end
